import { MiddlewareConsumer, Module, NestModule, RequestMethod, forwardRef} from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { LoggerMiddleware } from 'src/logger/logger.middleware'
import { User } from './entities/user.entity'
import { Tags } from './entities/tags.entity'
import { TypeOrmModule } from '@nestjs/typeorm';
import { JwtModule } from '@nestjs/jwt'
import { createClient } from 'redis'

@Module({
  imports: [TypeOrmModule.forFeature([User,Tags]),
  JwtModule.registerAsync({
    useFactory: async () => ({
      secret: 'zANDwNQVFzxlfG9myPxVWAkq4iXJEPhI',
      signOptions: {
        expiresIn: '12h',
      },
    })
  })],
  controllers: [UserController],
  providers: [UserService,{
    provide: 'REDIS_CLIENT',// 定义一个服务提供者
    // 创建 Redis 客户端实例
    async useFactory() {
      const client = createClient({
        socket: {
          host: 'localhost',
          port: 6379,
        }
      })
      await client.connect()
      return client;
    }
  }],
  exports:[UserService]
})
export class UserModule implements NestModule {
  configure (consumer:MiddlewareConsumer) {
      // consumer.apply(LoggerMiddleware).forRoutes('user')
      // consumer.apply(LoggerMiddleware).forRoutes({path:"user",method:RequestMethod.GET})
      // consumer.apply(LoggerMiddleware).forRoutes(UserController)
  }
}
